using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;

public partial class AdminPages_AddCourse : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        welcomeLabel.Text = "Please fill in all of the required information.";
    }
    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        SqlConnection conn;
        SqlCommand comm;

        string connectionString = ConfigurationManager.ConnectionStrings["Capstone"].ConnectionString;
        string query = "INSERT INTO Course (instructor_ID, course_Name, room_Num, credits, min_Student, max_Student, description) " +
            "VALUES (@instructor, @course, @room, @credit, @min, @max, @description )";

        conn = new SqlConnection(connectionString);
        comm = new SqlCommand(query, conn);

        try
        {
            conn.Open();

            String courseName = txtCourseName.Text;
            int roomNumber = Convert.ToInt16(txtRoomNum.Text);
            int credits = Convert.ToInt16(txtCredits.Text);
            String instructor = txtInstructor.Text;
            int minStudents = Convert.ToInt16(txtMinStudent.Text);
            int maxStudents = Convert.ToInt16(txtMaxStudent.Text);
            String description = txtDescription.Text;

            comm.Parameters.AddWithValue("@course", courseName);
            comm.Parameters.AddWithValue("@room", roomNumber);
            comm.Parameters.AddWithValue("@credit", credits);
            comm.Parameters.AddWithValue("@min", minStudents);
            comm.Parameters.AddWithValue("@max", maxStudents);
            comm.Parameters.AddWithValue("@instructor", instructor);
            comm.Parameters.AddWithValue("@description", description);

            if (comm.ExecuteNonQuery() > 0)
            {
                txtCourseName.Enabled = false;
                txtCredits.Enabled = false;
                txtDescription.Enabled = false;
                txtInstructor.Enabled = false;
                txtMaxStudent.Enabled = false;
                txtMinStudent.Enabled = false;
                txtRoomNum.Enabled = false;
                txtDescription.Enabled = false;
                btnSubmit.Enabled = false;
                btnCancel.Enabled = false;
                btnContinue.Visible = true;
               
                welcomeLabel.Text = "You have successfully added a new course to the curriculum.";
            }
            else
            {
                dbErrorLabel.Text = "Unable to add a class at this time.";
            }
        }
        catch
        {
            dbErrorLabel.Text = "Error inserting class into the database.";
        }
        finally
        {
            conn.Close();
            
        }
    }
    protected void btnCancel_Click(object sender, EventArgs e)
    {
        txtCourseName.Text = String.Empty;
        txtCredits.Text = String.Empty;
        txtInstructor.Text = String.Empty;
        txtMaxStudent.Text = String.Empty;
        txtMinStudent.Text = String.Empty;
        txtRoomNum.Text = String.Empty;
        txtDescription.Text = String.Empty;

    }
    protected void btnYes_Click(object sender, EventArgs e)
    {
        txtCourseName.Enabled = true;
        txtCredits.Enabled = true;
        txtDescription.Enabled = true;
        txtInstructor.Enabled = true;
        txtMaxStudent.Enabled = true;
        txtMinStudent.Enabled = true;
        txtRoomNum.Enabled = true;
        txtDescription.Enabled = true;
        btnSubmit.Enabled = true;
        btnCancel.Enabled = true;
        btnContinue.Visible = false;
        
    }


    protected void Button1_Click(object sender, EventArgs e)
    {
        btnContinue.Visible = true;
        btnSubmit.Enabled = true;
        btnCancel.Enabled = true;
        txtCourseName.Text = String.Empty;
        txtCredits.Text = String.Empty;
        txtInstructor.Text = String.Empty;
        txtMaxStudent.Text = String.Empty;
        txtMinStudent.Text = String.Empty;
        txtRoomNum.Text = String.Empty;
        txtDescription.Text = String.Empty;

    }
}
